<?php
declare (strict_types = 1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class Crud extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('crud')
            ->setDescription('根据类名生成对应模型和控制器及验证类')
            ->addArgument('app',  Option::VALUE_REQUIRED, '应用名称')
            ->addArgument('name',  Option::VALUE_REQUIRED, '模型名');
    }

    protected function execute(Input $input, Output $output)
    {
        $app = $input->getArgument('app');
        $name = $input->getArgument('name');
        $output->writeln($app.' '.$name);
//        // 生成控制器类
        $controllerContent = <<<EOF
<?php
namespace app\\{$app}\controller;

use app\\{$app}\\validate\\{$name}Validate;
use app\service\\traits\Crud;
use app\\{$app}\\model\\{$name} as {$name}Model;

class {$name} extends Base
{
    private {$name}Model \$model;

    use Crud;

    public function initialize()
    {
        parent::initialize();
        \$this->model         = new {$name}Model();
        \$this->validateClass = {$name}Validate::class;
    }
}
EOF;
        file_put_contents(app()->getAppPath() . "{$app}/controller/{$name}.php", $controllerContent);


        // 生成模型类

        $modelContent = <<<EOF
<?php
namespace app\\{$app}\model;


class {$name} extends Base
{

}
EOF;
        file_put_contents(app()->getAppPath() . "{$app}/model/{$name}.php", $modelContent);


        // 生成验证类
        $validateContent = <<<EOF
<?php
namespace app\\$app\\validate;


class {$name}Validate extends BaseValidate
{
    public function __construct()
    {
        \$rule = [

        ];

        \$message = [

        ];

        \$scene = [

        ];
        parent::__construct(\$rule, \$message, \$scene);
    }
}
EOF;
        file_put_contents(app()->getAppPath() . "{$app}/validate/{$name}Validate.php", $validateContent);

        $output->writeln('创建成功');
    }
}
